import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/HomeView.vue";
// import MainRecom from '../components/mainrecom/MainRecom.vue'

Vue.use(VueRouter);

const routes = [{
  path: "",
  component: Home,
  children: [{
      path: "/",
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () =>
        import( /* webpackChunkName: "main" */ "../components/MainRecom.vue"),
    },
    {
      path: "/hotrank",
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () =>
        import( /* webpackChunkName: "hot" */ "../components/HotRank.vue"),
    },
    {
      path: "/hotRankArticleDetail",
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () =>
        import(
          /* webpackChunkName: "hot" */
          "../components/HotRankArticleDetail.vue"
        ),
    },
    {
      path: "/mine",
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () =>
        import( /* webpackChunkName: "hot" */ "../components/Mine.vue"),
    },
    {
      path: "/comment",
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () =>
        import(
          /* webpackChunkName: "hot" */
          "../components/CommentDetail.vue"
        ),
    },
  ],
}, ];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  // to 将访问哪一个路径 
  // from 代表从哪个路径跳转而来
  // next 是一个函数,表示放行 http://realhole.pengdong.top/tweet/tweet_info/62
  //   next() 放行 next('/login') 强制跳转 /news /mine
  if (to.path === '/news' || to.path === '/mine' || to.path == '/hotrank') {
    // 如果用户未登录 不能查看
    if (!localStorage.user_id) {
      return Vue.prototype.$showMsg({
        msg: "请请先登录!",
        type: "warning"
      });
      return;
    }
  }

  // 路由拦截  分享文章重定向
  if (to.path.startsWith('/tweet/tweet_info')) {
    let pathArr = to.path.split('/');
    let id = pathArr[pathArr.length - 1]
    next('/comment?id=' + id)
    return;
  }

  if (to.path == '/news')
    return

  next()
})

export default router;